package com.example.myapplication;
import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import fi.iki.elonen.NanoHTTPD;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MyServer extends NanoHTTPD {

    private Context context;

    public MyServer(Context context) {
        super(8080);
        this.context = context;
    }

    @Override
    public Response serve(IHTTPSession session) {
        String uri = session.getUri();
        String msg = "<html><body><h1>Hello server</h1>\n";
        // 处理 GET 请求
        if (session.getMethod() == Method.GET) {
            switch (uri) {
                case "/hello":
                    return newFixedLengthResponse("Hello, World!");
                case "/time":
                    String currentTime = getCurrentTime();
                    return newFixedLengthResponse(currentTime);
                default:
                    return newFixedLengthResponse("Invalid path");
            }
        }

        // 处理 POST 请求
        if (session.getMethod() == Method.POST) {

            //post方式
            Log.e("MyServer","into POST");
            //JSONObject json = null;
            try {
                Map<String, String> files = new HashMap<String, String>();
                session.parseBody(files);
                String body=files.get("postData");

                JSONObject json = new JSONObject(body);

               String service_id = json.getString("service_id");
            //    String b = json.getString("b");
            //    String c = json.getString("c");
            //    String d = json.getString("d");
                Log.e("MyServer","json:"+ body);
                Log.e("MyServer","service_id:"+service_id );
                return newFixedLengthResponse("Upload successful");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ResponseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



  /*
           if ("/upload".equals(uri))
            {
                try {
                    // 解析 POST 请求的参数
                    Map<String, String> parms = session.getParms();
                    if (parms.get("username") == null) {
                        msg += "<form action='?' method='get'>\n  <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
                    } else {
                        msg += "<p>Hello, " + parms.get("username") + "!</p>";
                    }

                    return newFixedLengthResponse("Upload successful" +msg);
                } catch (Exception e) {
                    e.printStackTrace();

                    return newFixedLengthResponse("Upload failed: " + e.getMessage());
                }
            } */
        }

        return newFixedLengthResponse(Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not found");
    }

    private String getCurrentTime() {
        // 获取当前时间的逻辑
        // ...
        return "Current time: " + System.currentTimeMillis();
    }
}

